package com.multidisplay.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.multidisplay.bo.ResultBo;
import com.multidisplay.mapper.LoginLogMapper;
import com.multidisplay.mapper.VisitorLogMapper;
import com.multidisplay.po.LoginLog;
import com.multidisplay.po.User;
import com.multidisplay.po.VisitorLog;
import com.multidisplay.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private VisitorLogMapper visitorLogMapper;
    @Autowired
    private LoginLogMapper loginLogMapper;

    /**
     * 页面访问日志
     */
    @Override
    public void visitorLog(VisitorLog visitorLog){
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        String ipAddress = getIpAddress(request);
        HttpSession session = request.getSession();
        String flag = (String) session.getAttribute("flag");
        if(flag != "" && flag != null){
            visitorLog.setIp(ipAddress);
            visitorLog.setEquipmentFlag(flag);
            visitorLogMapper.insert(visitorLog);
        }
    }

    /**
     * 用户登录日志
     */
    @Override
    public void loginLog(HttpServletRequest request){
        HttpSession session = request.getSession();
        String ipAddress = getIpAddress(request);
        User user = (User) session.getAttribute("user");
        LoginLog loginLog = new LoginLog();
        loginLog.setUId(user.getuId());
        loginLog.setLoginTime(new Date());
        loginLog.setIp(ipAddress);
        loginLogMapper.insert(loginLog);
    }

    /**
     * 分页查询登陆日志
     */
    @Override
    public ResultBo<LoginLog> queryOfLogin(int page, int limit){
        IPage iPage = new Page(page,limit);
        IPage<LoginLog> loginLogIPage = loginLogMapper.selectPage(iPage,null);
        ResultBo<LoginLog> resultBo = new ResultBo<>();
        resultBo.setCode(0);
        resultBo.setMsg("");
        resultBo.setCount(loginLogIPage.getTotal());
        resultBo.setData(loginLogIPage.getRecords());
        return resultBo;
    }

    /**
     * 分页查询访问日志
     */
    @Override
    public ResultBo<VisitorLog> queryOfPage(int page, int limit){
        IPage iPage = new Page(page,limit);
        IPage<VisitorLog> visitorLogIPage = visitorLogMapper.selectPage(iPage,null);
        ResultBo<VisitorLog> resultBo = new ResultBo<>();
        resultBo.setCode(0);
        resultBo.setMsg("");
        resultBo.setCount(visitorLogIPage.getTotal());
        resultBo.setData(visitorLogIPage.getRecords());
        return resultBo;
    }

    /**
     * 获取用户IP地址
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 删除登入日志
     */
    @Override
    public void deleteLogin(LoginLog loginLog){
        loginLogMapper.deleteById(loginLog.getId());
    }

    /**
     * 删除访问日志
     */
    @Override
    public void deletePage(VisitorLog visitorLog){
        visitorLogMapper.deleteById(visitorLog.getId());
    }

}
